package org.jeecg.modules.paycenter.channel.offline;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.paycenter.enums.BillTypeEnum;
import org.jeecg.modules.paycenter.enums.PayChannelEnum;
import org.jeecg.modules.paycenter.enums.PaySourceEnum;
import org.jeecg.modules.paycenter.service.BasePayChannelService;
import org.jeecg.modules.paycenter.service.IPayChannelService;
import org.jeecg.modules.wlhy.bill.entity.HyBill;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customerwallet.entity.HyCustomerWallet;
import org.jeecg.modules.wlhy.customerwallet.service.IHyCustomerWalletService;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrier;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriverbank.entity.HyDriverBank;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class OfflinePayChannelService extends BasePayChannelService implements IPayChannelService {
    @Autowired private IHyCustomerWalletService customerWalletService;
    @Autowired private OfflineWalletService offlineBankService;


    //region base
    @Override
    public PayChannelEnum getChannelEnum() {
        return PayChannelEnum.OFFLINE;
    }

    @Override
    public boolean checkChannel() {
        return true;
    }

    @Override
    public String getChannel() {
        return PayChannelEnum.OFFLINE.channelType;
    }
    //endregion

    //region 支付

    @Override
    @Transactional
    public Result<?> billPayBatch(List<HyBill> hyBills, HyCustomer customer) {
        HyCustomerWallet customerWallet = customerWalletService.getByCustomerIdForUpdate(customer.getId());
        if (customerWallet == null) {
            return Result.error("货主系统钱包不存在");
        }
        if (customerWallet.izFrozen()) {
            return Result.error("货主系统钱包已冻结");
        }
        BigDecimal totalAmt = billService.getBillCustomerTotalFee(hyBills);
        //货主系统钱包余额
        if (!customerWallet.izEnoughAmt(totalAmt)) {
            return Result.error("系统钱包余额不足");
        }
        List<HyShippingNote> hyShippingNotes = billService.getBillShippingList(hyBills);
        Map<String, HyDriver> driverMaps = billService.getDriverMaps(hyShippingNotes); //司机
        Map<String, HyVehicle> vehicleMaps = billService.getVehicleMaps(hyShippingNotes);//车辆
        Map<String, HyCarrier> carrierMaps = billService.getCarrierMaps(hyShippingNotes);//车队长
        Map<String, HyDriverBank> driverBankMaps = billService.getDriverBankMaps(hyShippingNotes);

        Map<String, HyShippingNote> shippingNoteMap = billService.shippingListToMap(hyShippingNotes);
        for (HyBill hyBill : hyBills) {
            hyBill.setPaySource(PaySourceEnum.ACCOUNT_PAY.value);//支付源为货主账户余额
        }
        //系统钱包支付(线下支付)
        offlineBankService.payBatch(hyBills, customer, shippingNoteMap, driverMaps, driverBankMaps, carrierMaps, vehicleMaps, totalAmt);
        return Result.OK("支付成功");
    }

    @Override
    public Result<?> billPlateformPay(List<HyBill> hyBills, HyCustomer customer) {
        return Result.error("操作不可用,不支持此功能");
    }

    @Override
    public Result<?> agentDianFuPayBatch(BillTypeEnum billTypeEnum, List<HyBill> hyBills, HyCustomer agent, HyCustomer customer) {
        return Result.error("操作不可用,不支持此功能");
    }

    //endregion
}
